#include "StringParseUtil.h"

FormatedNumberList::FormatedNumberList( const char * numberListString, std::string format )
{
	m_format = format;

	std::string numberStr;
	UINT stringSize = (UINT)strlen( numberListString );

	for( UINT cnt = 0; cnt < stringSize; cnt++ )
	{
		char curChar = numberListString[cnt];

		if( curChar == ' ' || curChar == '\t' || curChar == '\n' || curChar == '\r' )
		{
			if( 0 < numberStr.size() )
			{
				InputNumber( numberStr.c_str() );
				numberStr.clear();
			}
		}
		else
		{
			numberStr += curChar;
		}
	}

	if( 0 < numberStr.size() )
	{
		InputNumber( numberStr.c_str() );
	}
}

void FormatedNumberList::Copy( void * dest )
{
	int size = GetNumberCount() * sizeof( DWORD );
	memcpy( dest, &m_numberList.front(), size );
}

BOOL FormatedNumberList::IsMatchInFormat( UINT index, char formatChar )
{
	int formatSize = (int)m_format.size();
	int formatIndex = index % formatSize;

	if( m_format[formatIndex] == formatChar )
	{
		return TRUE;
	}

	return FALSE;
}

void FormatedNumberList::InputNumber( const char * numberStr )
{
	int curIndex = GetNumberCount();

	if( IsMatchInFormat( curIndex, 'f' ) )
	{
		float number;
		sscanf( numberStr, "%f", &number );
		DWORD input = *((DWORD*)&number);
		m_numberList.push_back( input );
	}
	else if( IsMatchInFormat( curIndex, 'd' ) )
	{
		int number;
		sscanf( numberStr, "%d", &number );
		DWORD input = *((DWORD*)&number);
		m_numberList.push_back( input );
	}
	else if( IsMatchInFormat( curIndex, 'u' ) )
	{
		UINT number;
		sscanf( numberStr, "%u", &number );
		DWORD input = *((DWORD*)&number);
		m_numberList.push_back( input );
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////

IntegerList::IntegerList( const char * integerListString )
{
	std::string integerStr;
	UINT stringSize = (UINT)strlen( integerListString );

	for( UINT cnt = 0; cnt < stringSize; cnt++ )
	{
		char curChar = integerListString[cnt];

		if( curChar == ' ' || curChar == '\t' || curChar == '\n' || curChar == '\r' )
		{
			if( 0 < integerStr.size() )
			{
				int input = atoi( integerStr.c_str() );
				m_intList.push_back( input );
				integerStr.clear();
			}
		}
		else
		{
			integerStr += curChar;
		}
	}

	if( 0 < integerStr.size() )
	{
		int input = atoi( integerStr.c_str() );
		m_intList.push_back( input );
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////

FloatList::FloatList( const char * floatListString )
{
	std::string floatStr;
	UINT stringSize = (UINT)strlen( floatListString );

	for( UINT cnt = 0; cnt < stringSize; cnt++ )
	{
		char curChar = floatListString[cnt];

		if( curChar == ' ' || curChar == '\t' || curChar == '\n' || curChar == '\r' )
		{
			if( 0 < floatStr.size() )
			{
				float input = atof( floatStr.c_str() );
				m_floatList.push_back( input );
				floatStr.clear();
			}
		}
		else
		{
			floatStr += curChar;
		}
	}

	if( 0 < floatStr.size() )
	{
		float input = atof( floatStr.c_str() );
		m_floatList.push_back( input );
	}
}